// pages/doctor_side/index/index_selfprescribe/index_selfprescribe.js
import $service from '../../../../service.js';
const app = getApp()
var utils = require('../../../../utils/util.js');
var log_id; //问诊id
var log_type; //问诊类型
var isShow = true; //用户是否在线
var doctor_id; //医生id
var hospital_id = "1"; //医院id 
var hospitalizationList = {}; //住院列表
var inquiry_patient_id; //患者id
var isimType = true;
var backhome = 1 //医生是否退出问诊 1 未 2 是
var user_type;
var user_id;
var inquiryinfo = {};
var page = 0;
var isMeasured;
var thenS_;
var getMap; //获取用药map
var druglistStorage_z = []; //已选中药处方 列表
var druglistStorage_x = []; //已选西药处方 列表
var template_prescriptionList = {} //保存模板list
var dataId = ''; //检验检查
var checklistId; // 检验项目意见单 id
var checklistListAll; //检验检查
var StorageJosn; //生成处方
var prescriptionListlength = []; //已生成处方list
var template_id; //诊断id
var numText; //添加文字用 诊断
var bcmb_diagnosis_id;
// 西药开处方计算用Obj
var selectionunitNum = 0;
var medicineObj = null;
var tapMap_type;
var pageclose = true; //true //显示弹窗 false不显示弹窗
var rz_addTriageNote = true; //false 为分诊

let imId = ''; // 患者imID
let doctorIM = ''; // 医生imID
let logId = ''; // 问诊id
let inquiryPatientId = '';
let doctorId;
let _this;
let oosParams = {};
let chat = null;
let isNetWork = true
let isEnd = true
let patientName = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    msgList: [],
    userSig: null,
    userID: '', //账号
    userIDImg: '',
    selToID: "", //对方聊天账号
    selToIDImg: '',
    scrollTop: 0,
    increase: false, //图片添加区域隐藏
    aniStyle: true, //动画效果
    message: "",
    previewImgList: [],
    imUserInfo: {}, //im用户信息
    imSelToUserInfo: {}, //im对方用户信息
    isJoinClassroom: false, // 是否已经在课堂中
    template: '1v1bigsmall', //视频用
    smallViewLeft: 'calc(100vw - 20vw - 1vw)', //视频用
    smallViewTop: 'calc(1vw)', //视频用
    smallViewWidth: '20vw', //视频用
    smallViewHeight: '26vw',
    enableCamera: true, // 是否启用摄像头
    loadingImg: '',
    playerBackgroundImg: '',
    boardShowFullScreen: true, // 是否全屏显示白板
    boardHidden: true,
    islistshow: false, //常用语弹窗
    rtsytViewZt: true, //人体示意图显示隐藏
    log_type: 2,
    inputHeight: 0, //监听input弹起高度
    measuredDataList: [], //测量数据list
    measuredDataMap: {}, //测量数据map
    num: 0, //测量数据
    loadingText: '',
    loadingType: true,
    timeList: [], //图片列表
    wzlslist: [], //问诊历史list
    inquiryMap: {}, //患者信息
    prescriptionList: [], //已开处方列表
    drug_type: 1, //处方类型 1中药 2 西药
    tabactive: '', //处方诊断 //0过敏史 1 主诉 2现病史 3既往史 4诊断
    type: 0,
    openPageBox: 9, //打开弹窗  // 1查询病历 2检验检查 3健康指导意见单 4查询图片 5人体示意 6住院建议书 7问诊规范 8结束问诊 9开处方 10修改患者资料 11分诊  12常用语 13 测量数据 14查询历史问诊 15开处方-选择开药 16 开处方-生成处方 17检验检查-心电图 18检验检查-申请单  19健康指导-健康指导意见单详情填写 20填写诊断信息、主诉、过敏史等 21药品信息 22 保存模板 23处方详情 24 检验检查详情 25健康指导详情 26住院建议详情
    openPageBox_up: 0, //上级的 openPageBox
    tapactive: 1, //处方选项卡
    drugList: [], //处方 目录、分类
    drugList2: [], //处方 个人用药
    drugList3: [], //处方 公共模板 、个人模板
    drugList4: [], //处方 历史处方
    drug_name: '', //处方  请输入药品名称或首字母
    class_name: '', // 处方 分类选择框
    drugClassList: [], //处方 药品分类列表
    isshow: false, //处方 选择分类弹窗显示隐藏
    class_id: '', //处方 分类id
    isdetalis: false, //处方 查看详情
    diagnosisMap: [], //处方 模板详情
    prescriptionMap: {}, //处方 模板详情列表
    drugMap: {}, //开药详情
    consumptionlist: [], //用量单位list
    useUnit: "", //默认用量单位
    list: [], //用法list
    list2: [], //频次list
    list3: [], //嘱托list
    details_usage: '', //用法
    details_consumption: '', //用量
    details_consumption_util: '', //用量单位
    details_frequency: '', ///频率,
    details_frequency_str: '',
    details_frequency_str2: '',
    details_day: '', //天数
    details_num: '', //盒数
    storeName: '',
    storeId: '', //药店ID
    shopId: '', //搜索时药店ID
    islist: false, //弹窗显示隐藏
    islist2: false, //弹窗显示隐藏
    islist3: false, //弹窗显示隐藏
    isconsumptionlist: false, //弹窗显示隐藏
    drug_packaging_unit: '', //盒数单位
    getdruglistStoragelist: [], //已选处方
    sccfisshow: 0, //开药显示
    prescription_price: 0, //处方金额
    prescription_taking: '', //服用方法
    prescription_requirements: '', //服用要求
    prescription_num: '', //付数
    prescription_isdetalis: false, //生成中药弹窗
    frequency_num: 0,
    frequency_day_num: 0,
    selectionunit: 0,
    drug_packaging_num: 1,
    isClassList: 1, //检验检查
    checklistClassList: [], // 检验检查 大分类列表
    nextClassList: [], //检验检查 二分类列表
    checklistList: [], //检验检查 三分类列表
    checkThreeList: [], //检验检查 四分类列表
    isThree: false, // 是否有三级菜单
    dataId: '', //一级
    dataId2: '', //二级
    lof_proposal: '', //建议检查 医生建议
    checklist_objMap: '', //验检查申请单内容
    checklist_name: '', //检验检查项目名字
    checklist_last_code: '', //上次单号
    checklist_abstract: '', //摘要
    checklist_sbp: '', //收缩压
    checklist_dbp: '', //舒张压
    lof_diagnosis: '', //临床诊断
    is_digitalization: 0, //是否使用过洋地黄  0否 1是
    checklist_digitalization_num: '', //使用洋地黄用量
    checklist_digitalization_start: '', //使用洋地黄开始时间
    checklist_digitalization_end: '', //使用洋地黄结束时间
    is_other_drugs: 0, //是否使用有关药物    0否 1是
    checklist_other_drugs_name: '', //有关药物名称
    checklist_other_drugs_start: '', //有关药物开始时间
    checklist_other_drugs_end: '', //有关药物结束时间
    checklist_other_drugs_num: '', //有关药物用量
    checklist_inspection_result: '', //有关检验结果
    bz: '', //备注
    beforeYear: '', //使用洋地黄开始时间 年
    beforeMon: '', //使用洋地黄开始时间 月
    beforeDay: '', //使用洋地黄开始时间 日
    beforeYear2: '', //有关药物开始时间 年
    beforeMon2: '', //有关药物开始时间 月
    beforeDay2: '', //有关药物开始时间 日
    newYear: '', //使用洋地黄结束时间 年
    newMon: '', //使用洋地黄结束时间 月
    newDay: '', //使用洋地黄结束时间 日
    newYear2: '', //有关药物结束时间 年
    newMon2: '', //有关药物结束时间 月
    newDay2: '', //有关药物结束时间 日
    jyjcXdbisshow: false, //心电图
    jyjcXdb_isClassList: 1,
    jyjcXdb_nextClassList: [], //一级列表
    jyjcXdb_checklistList: [], //二级列表
    jyjcXdb_opinionList: [], //已选中检验项目
    jyjcXdb_dataId2: '',
    jyjcXdb_checklist_abstract: '', //摘要
    jyjcXdb_checklist_inspection_result: '', //有关检验结果
    jyjcXdb_bz: '', //备注
    health_active: true, //健康指导选项卡
    guidance_name: '', //健康指导内容搜索
    carouselActivityList: [], //健康指导列表
    health_isdetalis: false, //弹窗
    guidance_nameText: '', //弹窗内容
    jkzdxqtx_active: true, //填写健康指导意见单
    hiddenName: true, //下拉框内容
    selected: 1, //填写健康指导意见单选项卡
    hiddenpop: true, //弹窗
    showModal: false,
    guidance_content: '', //输入内容
    doctorMap: {}, //医生资料
    guidanceList: [], //指导list
    jkzdxqtx_isdetalis: false, //弹窗
    jkzdxqtx_guidance_nameText: '', //弹窗内容
    zyjys_bz: '', //健康指导备注
    wzgf_active: 1, //问诊规范
    wzgf_list: [], //问诊规范 问诊流程
    wzgf_list2: [], //药品规范
    wzgf_list3: [], //诊断规范
    wzgf_queryName: '', //问诊话术搜素
    wzgf_isdetalis: false, //问诊规范 弹窗
    wzgf_drugMap: {}, //问诊规范弹窗内容
    incompatibilityList: [], //药品冲突list
    tabactive: 1, //填写诊断。主诉 。过敏史 选项卡
    templateList: [], //list
    mould: 0, //模板选项卡
    text: '', //显示内容
    txdiagnosis_iinquiry_patient_allergy_details: '', //过敏史
    txdiagnosis_log_main_suit: '', //主诉
    txdiagnosis_log_present_illness: '', //现病史
    txdiagnosis_log_history_present_illness: '', //既往史
    txdiagnosis_lof_diagnosis: '', //诊断
    txdiagnosis_isdetalis: false,
    txdiagnosis_isdetalistype: 1,
    template_type: '', //模板类型
    template_name: '', //模板名称
    template_details: '', //模板详情
    template_list: ['主诉', '现病史', '既往史'],
    txdiagnosis_islist2: false,
    txdiagnosis_isdiagnosis: false, //自定义诊断弹窗
    isdiagnosisText: '', //自定义诊断
    txdiagnosis_name: '', //搜索
    diagnosis_list: [], //已开诊断列表
    activeIndex: '', //诊断列表选中下标
    inputtext: '', //补充的值
    ypxx_drugMap: {}, //药品信息
    xghzzl_isshow: false, //职业选择窗口
    starttime: '', //日期开始时间
    endtime: '', //结束时间
    xghzzl_list: [], // 职业列表
    bcmb_icd_id: '', //保存模板分类id
    bcmb_icd_name: '', //分类名称
    bcmb_bz: '', //备注
    bcmb_prescription_price: '0', //处方价格
    bcmb_prescription_name: '', //处方名称
    bcmb_prescription_taking: '', //服用方法
    bcmb_prescription_requirements: '', //服用要求
    bcmb_prescription_num: '', //付数
    bcmb_list: [],
    bcmb_list2: [],
    bcmb_diagnosisList: [],
    bcmb_queryName: '',
    bcmb_isullist: false,
    bcmb_type: 0,
    prescription_imgurl: '', //处方详情 图片
    checklist_imgurl: '', //检验检查详情 图片
    guidance_imgurl: '', //健康指导详情 图片
    application_imgurl: '', //申请单详情 图片
    hospitalization_imgurl: '', //住院建议详情 图片
    bg_view_show: false, //输入框 解决等级高问题
    isinputShowed: false, //输入框 解决等级高问题
    inputShowed: false, //输入框 解决等级高问题
    isinputShowed2: false, //输入框 解决等级高问题
    inputShowed2: false, //输入框 解决等级高问题
    isinputShowed3: false, //输入框 解决等级高问题
    inputShowed3: false, //输入框 解决等级高问题
    isinputShowed4: false, //输入框 解决等级高问题
    inputShowed4: false, //输入框 解决等级高问题
    isinputShowed5: false, //输入框 解决等级高问题
    inputShowed5: false, //输入框 解决等级高问题
    isinputShowed6: false, //输入框 解决等级高问题
    inputShowed6: false, //输入框 解决等级高问题
    isinputShowed7: false, //输入框 解决等级高问题
    inputShowed7: false, //输入框 解决等级高问题
    isdatalook: false, //测量数据
    isimglook: false, //查询图片
    fzdoctorList: [], //分诊 医生列表
    fzisshow: false, //弹窗
    fzactivenum: -1,
    fzdepartmentList: [],
    fzdepartment_id: '',
    fzdoctor_name: '',
    syncFlag: true, //同步处方按钮显示
    prescription_id: "",
    noPrescriptionList: [], //未开方原因列表
    reasonsName: '其他', //未开发原因
    isWithdrawShow: false, // 是否展示撤回弹窗
    toView: '',
    messageHeight: '', // 底部操作栏高度
    myIM: '', // 自己的IMid
    friendIM: '', // 对方的IMid
    userName: '', // 自己的名称
    doctorStoreList: [], //药店列表
    valueIndex: 0, //中药分类选择index
    wmValueIndex: 0, //西药分类选择index
    storeIndex: 0, //药店来源列表index
    wmstoreIndex: 0, //西药药店来源列表index
    scrollTop: 0 //滚动条高度
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    console.log(e)
    // imId = e.imId;
    // doctorIM = e.doctorIM;
    // inquiryPatientId = e.inquiryPatientId;
    // patientName = e.patientName
    // wx.setNavigationBarTitle({
    //   title: e.patientName
    // })
    doctorId = JSON.parse($service.getStore('userInfo')).id
    // ---------------
    // log_id = e.log_id
    // log_type = e.log_type
    this.setData({
      // friendIM: e.imId,
      // myIM: e.doctorIM,
      // log_type: e.log_type,
      userName: JSON.parse($service.getStore('userInfo')).trueName
    })
    var that = this

    if (!$service.isUndefinedAndEmpty($service.getUserLocalStorage().hospital_id)) {
      hospital_id = $service.getUserLocalStorage().hospital_id
    }
    doctor_id = $service.getUserLocalStorage().doctor_id
    this.publicUtil_getInquiryLogDetails()
    this.getBeforeDate(14);
    var d = new Date();
    var year = d.getFullYear();
    var mon = d.getMonth() + 1;
    mon = mon < 10 ? ('0' + mon) : mon;
    var day = d.getDate();
    day = day < 10 ? ('0' + day) : day;
    this.setData({
      beforeYear2: year,
      beforeMon2: mon,
      beforeDay2: day,
      newYear: year,
      newMon: mon,
      newDay: day,
      newYear2: year,
      newMon2: mon,
      newDay2: day,
      loadingImg: '',
      playerBackgroundImg: '',
      boardShowFullScreen: true, // 是否全屏显示白板
      boardHidden: true,
      islistshow: false, //常用语弹窗
      rtsytViewZt: true //人体示意图显示隐藏

    })
    this.ossPolicy()
    wx.hideHomeButton()
    this.getDoctorStoreList() //获取药店列表
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  // 打开弹窗
  openPage(e) { // 1查询病历 2检验检查 3健康指导意见单 4查询图片 5人体示意 6住院建议书 7问诊规范 8结束问诊 9开处方 10修改患者资料 11分诊  12常用语 13 测量数据 14查询历史问诊 15开处方-选择开药 16 开处方-生成处方 17检验检查-心电图 18检验检查-申请单  19健康指导-健康指导意见单详情填写 20填写诊断信息、主诉、过敏史等 21药品信息 22 保存模板 23处方详情 24 检验检查详情 25健康指导详情 26住院建议详情
    this.setData({
      isWithdrawShow: false
    }) // 隐藏撤回弹窗
    var index = e.currentTarget.dataset.index;
    this.publicUtil_getInquiryLogDetails()
    if (index == 2) { // 检验检查
      this.setData({
        isClassList: 1, //检验检查
        checklistClassList: [], // 检验检查 大分类列表
        nextClassList: [], //检验检查 二分类列表
        checklistList: [], //检验检查 三分类列表
        dataId: '', //一级
        dataId2: '', //二级
      })
      this.doctorSide_personal_inquiry_getUtilChecklistClass();
    } else if (index == 3) { // 3健康指导意见单
      this.getHealthGuidanceModelList()
    } else if (index == 4) { //4查询图片
      this.getChatImages(logId)
      this.setData({
        isimglook: false //查询图片
      })
    } else if (index == 5) { // 5人体示意
      this.openTeduBoard()
      isimType = false
      if (this.data.boardHidden == true) {
        this.data.rtsytViewZt = true;
        var sendSystemMessageMsgText = '医生打开了人体示意图'
        wx.txTic.sendCustomMessage(this.data.selToID, 1)
        wx.txTic.sendSystemMessageMsg(this.data.selToID, sendSystemMessageMsgText);
      } else {
        // this.data.teduBoard.clear(false);
        this.data.rtsytViewZt = false;
      }
      this.setData({
        boardHidden: !this.data.boardHidden,
        rtsytViewZt: this.data.rtsytViewZt
      })
      // this.bottom()
    } else if (index == 6) { //住院建议书
      thenS_.openPageBox_up = index;
      thenS_.openPageBox = index;
      if (!hospitalizationList) {
        thenS_.openPageBox = index;
      } else {
        console.log(hospitalizationList)
        thenS_.openPageBox = 26;
        this.getEstablishPdf(inquiryPatientId, 1)
      }
      this.setData({
        openPageBox: thenS_.openPageBox,
        openPageBox_up: thenS_.openPageBox_up
      })
    } else if (index == 7) { //7问诊规范
      page = 1
      this.setData({
        wzgf_active: "1"
      })
      this.doctorSide_personal_inquiry_getSpecification()
    } else if (index == 8) { //结束问诊
      thenS_.openPageBox = 27;
      this.noPrescriptionList();
      this.setData({
        openPageBox: thenS_.openPageBox,
      })
    } else if (index == 10) { //修改患者资料
      this.publicUtil_getUtilTable10()
    } else if (index == 11) {
      this.getDepartmentListPc();
      this.getOnlineDoctorList();
    } else if (index == 12) { //常用语
      let params = {
        "current": 1,
        "modelName": "",
        "size": 5
      }
      app.http.getDoctorNormalConsultationList(params).then(res => {
        this.setData({
          islistshow: !this.data.islistshow,
          list: res.data.list
        })
      })
    } else if (index == 13) { //测量数据
      page = 1
      this.publicUtil_getPatientMeasuredDataList('now');
      this.publicUtil_getPatientMeasuredDataList('history');
      this.setData({
        isdatalook: false //测量数据
      })

    }
    if (index != 5 && index != 6 && index != 8 && index != 12) {
      thenS_.openPageBox_up = thenS_.openPageBox;
      this.setData({
        openPageBox: index,
        openPageBox_up: thenS_.openPageBox_up
      })
      this.getpageScrollTo()
    }
  },
  //开处方、
  //数据
  gotodetalis(e) {
    var index = e.currentTarget.dataset.index;
    thenS_.openPageBox_up = thenS_.openPageBox;
    if (index == 0 || index == 1 || index == 2 || index == 3 || index == 4) { //0过敏史 1 主诉 2现病史 3既往史 4诊断
      thenS_.openPageBox = 20
      thenS_.tabactive = index;
      if (e.index == 0) {
        this.data.text = thenS_.inquiryMap.patientAllergyDetails
      } else if (e.index == 1) {
        this.data.text = thenS_.inquiryMap.mainSuit
      } else if (e.index == 2) {
        this.data.text = thenS_.inquiryMap.presentIllness
      } else if (e.index == 3) {
        this.data.text = thenS_.inquiryMap.historyPresentIllness
      } else if (e.index == 4) {}
      this.setData({
        tabactive: thenS_.tabactive,
        text: this.data.text,
        txdiagnosis_iinquiry_patient_allergy_details: thenS_.inquiryMap.patientAllergyDetails, //过敏史
        txdiagnosis_log_main_suit: thenS_.inquiryMap.mainSuit, //主诉
        txdiagnosis_log_present_illness: thenS_.inquiryMap.presentIllness, //现病史
        txdiagnosis_log_history_present_illness: thenS_.inquiryMap.historyPresentIllness, //既往史
        txdiagnosis_lof_diagnosis: thenS_.inquiryMap.diagnosis //诊断
      })
      this.getdoctorSide_personal_inquiry()
      this.getlofdiagnosis(thenS_.inquiryMap.diagnosis, 1)
    } else if (index == 5) {
      var num = e.currentTarget.dataset.num;
      thenS_.openPageBox = 23;
      this.setData({
        syncFlag: true
      })
      this.getEstablishPdf(thenS_.prescriptionList[num].id, 4)
      // this.publicUtil_getInquiryLogPrescriptionDetails(thenS_.prescriptionList[num].id)
    } else if (index == 6 || index == 7) { //中西药开方
      // if ($service.isUndefinedAndEmpty(thenS_.inquiryMap.mainSuit)) {
      //   $service.showToast("请先输入主诉")
      //   return;
      // }
      // if ($service.isUndefinedAndEmpty(thenS_.inquiryMap.diagnosis)) {
      //   $service.showToast("请先输入诊断")
      //   return;
      // }
      if (index == 6) {
        thenS_.drug_type = "2"

      } else {
        thenS_.drug_type = "1"
      }
      thenS_.openPageBox = 15;
    }

    if (index == 6 || index == 7) {
      // 重置药品筛选条件
      this.setData({
        drug_name: '',
        class_name: '',
        class_id: '',
        tapactive: "1",
        shopId: ''
      })
      page = 1
      this.getalldo()
    }
    this.setData({
      openPageBox: thenS_.openPageBox,
      openPageBox_up: thenS_.openPageBox_up,
      drug_type: thenS_.drug_type,
    })
    this.getpageScrollTo()
  },
  //删除处方
  deletImg(e) {
    var this_ = this
    var index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '温馨提示',
      content: '确定要将此处方作废？',
      success(res) {
        if (res.confirm) {
          console.log(thenS_.prescriptionList[index].id)
          app.http.deletePrescription({
            id: thenS_.prescriptionList[index].id
          }).then(res => {
            $service.showToast(res.msg)
            this_.publicUtil_getInquiryLogDetails()
          }).catch(err => {
            $service.showToast(err.msg)
          })
        }
      }
    })
  },
  //填写诊断、主诉、过敏史等
  getdoctorSide_personal_inquiry() { //index 0过敏史 1 主诉 2现病史 3既往史 4诊断
    if (thenS_.tabactive == 0) {
      thenS_.text = thenS_.txdiagnosis_iinquiry_patient_allergy_details
    } else if (thenS_.tabactive == 1) {
      thenS_.text = thenS_.txdiagnosis_log_main_suit
    } else if (thenS_.tabactive == 2) {
      thenS_.text = thenS_.txdiagnosis_log_present_illness
    } else if (thenS_.tabactive == 3) {
      thenS_.text = thenS_.txdiagnosis_log_history_present_illness
    }
    this.setData({
      text: thenS_.text
    })
    if (thenS_.tabactive == 1 || thenS_.tabactive == 2 || thenS_.tabactive == 3) {
      this.getTemplate();
      // this.doctorSide_personal_inquiry_getDoctorTemplate()
    } else if (thenS_.tabactive == 4) {
      // this.doctorSide_personal_prescription_getDiagnosisList2()
      this.getIcdList();
    }
  },
  //诊断遍历
  getlofdiagnosis(diagnosisText, num) {
    var nameList = [];
    var numC = 0

    if (num == 1) { //map
      if (!$service.isUndefinedAndEmpty(diagnosisText)) {
        var lofdiagnosislist = diagnosisText.split(",")
        for (var i = 0; i < lofdiagnosislist.length; i++) {
          var index = lofdiagnosislist[i].indexOf('自定义诊断');
          if (index > -1) {
            var json = {
              "name": lofdiagnosislist[i],
              "type": "1",

            }
          } else {
            numC++
            var json = {
              "name": lofdiagnosislist[i],
              "type": "2",
              "num": numC
            }
          }
          nameList.push(json);
        }
      }
    } else if (num == 2) { //list
      for (var i = 0; i < diagnosisText.length; i++) {
        if (diagnosisText[i].type == "1") {
          var json = {
            "name": diagnosisText[i].name,
            "type": "1",

          }
        } else {
          numC++
          var json = {
            "name": diagnosisText[i].name,
            "type": "2",
            "num": numC
          }
        }
        nameList.push(json);
      }

    }
    this.setData({
      diagnosis_list: nameList,
    })


  },
  //返回顶部
  getpageScrollTo() {
    wx.pageScrollTo({
      scrollTop: 0
    })
  },
  // 主诉 2现病史 3既往史 模版
  getTemplate() {
    let params = {
      "isPersonalTemplate": Number(thenS_.mould),
      "templateName": thenS_.txdiagnosis_name,
      "templateType": thenS_.tabactive, //1 主诉 2现病史 3既往史
    }
    app.http.getTemplate(params).then(res => {
      this.setData({
        templateList: res.data.list
      })
    }).catch(err => {
      $service.showToast(err.msg)
    })
  },
  // 诊断列表
  getIcdList() {
    app.http.getIcdList({
      keyword: thenS_.txdiagnosis_name
    }).then(res => {
      this.setData({
        templateList: res.data.list
      })
    }).catch(err => {
      $service.showToast(err.msg)
    })
  },
  //问诊详情
  publicUtil_getInquiryLogDetails: function () {
    let _this = this;
    let arr = []
    // _this.setData({
    //   inquiryMap: data,
    //   selToIDImg: data.patientImg,
    //   userIDImg: JSON.parse($service.getStore('doctorInfo')).doctorHeadImg,
    //   prescriptionList: data.prescriptionList,
    //   guidanceList: data.inquiryLogHealthGuidanceList,
    //   doctorMap: JSON.parse($service.getStore('doctorInfo'))
    // })

  },
  //日期时间计算
  getBeforeDate(n, num) {
    var n = n;
    var d = new Date();
    var year = d.getFullYear();
    var mon = d.getMonth() + 1;
    var day = d.getDate();
    if (day <= n) {
      if (mon > 1) {
        mon = mon - 1;
      } else {
        year = year - 1;
        mon = 12;
      }
    }
    d.setDate(d.getDate() - n);
    year = d.getFullYear();
    mon = d.getMonth() + 1;
    mon = mon < 10 ? ('0' + mon) : mon
    day = d.getDate();
    day = day < 10 ? ('0' + day) : day,
      this.setData({
        beforeYear: year,
        beforeMon: mon,
        beforeDay: day
      })

  },
  // oss信息
  ossPolicy() {
    app.http.ossPolicy().then(res => {
      oosParams = res.data;
    })
  },
  //获取药店列表
  getDoctorStoreList() {
    app.http.getStoreList('').then(res => {
      res.data.unshift({
        group_name: '全部'
      })
      this.setData({
        doctorStoreList: res.data
      })
    })
  },
  // 关闭弹窗
  closeBox(e, num) {
    if (num == 1) {
      var index = 2; //1关闭全部弹窗   2关闭当前弹窗
    } else {
      var index = e.currentTarget.dataset.index; //1关闭全部弹窗   2关闭当前弹窗
    }
    console.log(index)
    // 1查询病历 2检验检查 3健康指导意见单 4查询图片 5人体示意 6住院建议书 7问诊规范 8结束问诊 9开处方 10修改患者资料 11分诊  12常用语 13 测量数据 
    //14查询历史问诊 15开处方-选择开药 16 开处方-生成处方 17检验检查-心电图 18检验检查-申请单  19健康指导-健康指导意见单详情填写 20填写诊断信息、主诉、过敏史等 21药品信息 22 保存模板 23处方详情 24 检验检查详情 25健康指导详情 26住院建议详情
    var This_openPageBox = 0
    if (index == 1) {
      This_openPageBox = 9
    } else {
      if (thenS_.openPageBox == 14) { //查询历史问诊  返回上一弹窗 测量数据
        This_openPageBox = 1
      } else if (thenS_.openPageBox == 15) { //开处方-选择开药  返回上一弹窗 开处方
        This_openPageBox = 9
      } else if (thenS_.openPageBox == 16) { //开处方-生成处方  返回上一弹窗 开处方-选择开药
        This_openPageBox = 15
      } else if (thenS_.openPageBox == 17 || thenS_.openPageBox == 18) { //检验检查-申请单  返回上一弹窗 检验检查
        This_openPageBox = 2
      } else if (thenS_.openPageBox == 19) { //
        This_openPageBox = 3
      } else if (thenS_.openPageBox == 20 || thenS_.openPageBox == 21 || thenS_.openPageBox == 22 || thenS_.openPageBox == 23 || thenS_.openPageBox == 24 || thenS_.openPageBox == 25 || thenS_.openPageBox == 28) { // 返回上一弹窗 
        This_openPageBox = thenS_.openPageBox_up
        // 判断是否有住院建议书备注
        if (thenS_.openPageBox == 20) {
          let bz = $service.getStore('zyjys_bz');
          if (bz) {
            this.setData({
              zyjys_bz: bz
            })
          }
        }
      } else {
        // 清楚住院建议书备注缓存
        if (thenS_.openPageBox == 6) {
          $service.removeStore('zyjys_bz')
        }
        This_openPageBox = 0
      }
    }
    if (This_openPageBox == 0) {
      this.bottom()
    }
    this.setData({
      openPageBox: This_openPageBox,
      bcmb_type: 0,
      bcmb_icd_id: '', //保存模板分类id
      bcmb_icd_name: '', //分类名称
      bcmb_bz: '', //备注
      bcmb_prescription_price: '0', //处方价格
      bcmb_prescription_name: '', //处方名称
      bcmb_prescription_taking: '', //服用方法
      bcmb_prescription_requirements: '', //服用要求
      bcmb_prescription_num: '', //付数
      bcmb_list: [],
      bcmb_list2: [],
      bcmb_diagnosisList: [],
      bcmb_queryName: '',
      bcmb_isullist: false
    })
  },
  //聊天消息始终显示最底端
  bottom() {
    this.setData({
      toView: 'flag'
    })
  },
  //选中诊断列表
  templateListtap(e) {
    var index = e.currentTarget.dataset.index; //
    var num = 0;
    // debugger
    for (var i = 0; i < thenS_.diagnosis_list.length; i++) {
      if (thenS_.diagnosis_list[i].type == 2) {
        num++
      }
      if (thenS_.diagnosis_list[i].name == thenS_.templateList[index].icdName) {
        $service.showToast('有重复诊断!');
        return
      }
    }
    var json = {
      "name": thenS_.templateList[index].icdName,
      "diagnosisId": thenS_.templateList[index].id,
      "type": "2",
      "num": (num + 1)
    }
    thenS_.diagnosis_list.push(json)
    numText = json.name
    this.setData({
      activeIndex: index, //
      diagnosis_list: thenS_.diagnosis_list
    })

  },

  // 诊断 主诉 现病史 既往史 按钮
  async txdiagnosis_btn(e) {
    var this_ = this
    var index = e.currentTarget.dataset.index; //1清空 2保存模板() 3保存
    if (index == 1) {
      if (thenS_.tabactive == 0) {
        thenS_.txdiagnosis_iinquiry_patient_allergy_details = ''
      } else if (thenS_.tabactive == 1) {
        thenS_.txdiagnosis_log_main_suit = ''
      } else if (thenS_.tabactive == 2) {
        thenS_.txdiagnosis_log_present_illness = ''
      } else if (thenS_.tabactive == 3) {
        thenS_.txdiagnosis_log_history_present_illness = ''
      } else if (thenS_.tabactive == 4) {
        thenS_.diagnosis_list = []
      }
      this.setData({
        txdiagnosis_iinquiry_patient_allergy_details: thenS_.txdiagnosis_iinquiry_patient_allergy_details, //过敏史
        txdiagnosis_log_main_suit: thenS_.txdiagnosis_log_main_suit, //主诉
        txdiagnosis_log_present_illness: thenS_.txdiagnosis_log_present_illness, //现病史
        txdiagnosis_log_history_present_illness: thenS_.txdiagnosis_log_history_present_illness, //既往史
        diagnosis_list: thenS_.diagnosis_list //诊断
      })
      this.getdoctorSide_personal_inquiry()
    } else if (index == 2) { //保存模板
      this.setData({
        txdiagnosis_isdetalis: true,
        txdiagnosis_isdetalistype: 1,
        template_type: '', //模板类型
        template_name: '', //模板名称
        template_details: '' //模板内容
      })
    } else if (index == 3) { //保存
      if (thenS_.tabactive == 0) { //药物过敏史
        this.updateAllergyHistory();
      } else if (thenS_.tabactive == 1 || thenS_.tabactive == 2 || thenS_.tabactive == 3) { //主诉、现病史、既往史 
        this.updateInquiryLog();
      } else if (thenS_.tabactive == 4) { //诊断
        this.updateDiagnosis(thenS_.diagnosis_list);
      }
      this_.closeBox('', 1); //关闭窗口
    } else if (index == 4) { //取消生成个人模板
      this.setData({
        txdiagnosis_isdetalis: false,
        txdiagnosis_isdiagnosis: false,
        isdiagnosisText: thenS_.isdiagnosisText
      })
    } else if (index == 5) { //确认生成 编辑个人模板
      if ($service.isUndefinedAndEmpty(thenS_.template_type)) {
        $service.showToast('请选择模板类型');
        return;
      }
      if ($service.isUndefinedAndEmpty(thenS_.template_name)) {
        $service.showToast('请输入模板名称');
        return;
      }
      if ($service.isUndefinedAndEmpty(thenS_.template_details)) {
        $service.showToast('请输入模板内容');
        return;
      }
      if (thenS_.template_type == "主诉") {
        var template_type = "1"
      } else if (thenS_.template_type == "现病史") {
        var template_type = "2"
      } else if (thenS_.template_type == "既往史") {
        var template_type = "3"
      }
      let params = {}
      if (thenS_.txdiagnosis_isdetalistype == 1) {
        params = {
          "templateType": template_type,
          "templateName": thenS_.template_name,
          "templateDetails": thenS_.template_details
        }
      } else {
        params = {
          "id": template_id,
          "templateType": template_type,
          "templateName": thenS_.template_name,
          "templateDetails": thenS_.template_details
        }
      }
      console.log(params)
      app.http.newTemplate(params).then(res => {
        $service.showToast(res.msg)
        this_.setData({
          template_type: '', //模板类型
          template_name: '', //模板名称
          template_details: '', //模板内容
          txdiagnosis_isdetalis: false
        })
        this_.getdoctorSide_personal_inquiry()
      }).catch(err => {
        $service.showToast(err.msg)
      })
    } else if (index == 6) { //打开自定义诊断
      for (var i = 0; i < thenS_.diagnosis_list.length; i++) {
        if (thenS_.diagnosis_list[i].type == "1") {
          thenS_.isdiagnosisText = thenS_.diagnosis_list[i].name.substring(6, 1000)
        }
      }
      this.setData({
        txdiagnosis_isdiagnosis: true,
        isdiagnosisText: thenS_.isdiagnosisText
      })
    } else if (index == 7) { //保存诊断
      var num = 0
      for (var i = 0; i < thenS_.diagnosis_list.length; i++) {
        if (thenS_.diagnosis_list[i].type == "1") {
          num = 1
          thenS_.diagnosis_list[i].name = '自定义诊断：' + thenS_.isdiagnosisText
        }
      }
      if (num == 0) {
        var json = {
          "name": '自定义诊断：' + thenS_.isdiagnosisText,
          "type": "1",
          "diagnosisId": 0,
        }
        thenS_.diagnosis_list.push(json)
      }
      this.setData({
        txdiagnosis_isdiagnosis: false,
        diagnosis_list: thenS_.diagnosis_list
      })
    }
  },
  // 保存诊断
  updateDiagnosis(list) {
    let arr = []
    list.forEach((item) => {
      item.diagnosis = item.name
      arr.push(item.name)
    })
    let params = {
      "diagnosis": list,
      "id": logId, //logId
    }
    app.http.updateDiagnosis(params).then(res => {
      $service.showToast(res.msg);
      this.data.inquiryMap.diagnosis = arr.join();
      this.setData({
        'inquiryMap.diagnosis': this.data.inquiryMap.diagnosis
      })
      this.publicUtil_getInquiryLogDetails();
    }).catch(err => {
      $service.showToast(err.msg);
    })
  },
  //删除诊断列表
  delImg(e) {
    var index = e.currentTarget.dataset.index; //
    var list = thenS_.diagnosis_list
    for (var i = 0; i < thenS_.diagnosis_list.length; i++) {
      if (index == i) {
        if (thenS_.diagnosis_list[i].type == "1") {
          thenS_.isdiagnosisText = ''
        }
        list.splice(i, 1)
      }
    }
    this.setData({
      isdiagnosisText: thenS_.isdiagnosisText
    })
    this.getlofdiagnosis(list, 2)
  },

  // 输入框监听
  txdiagnosis_inputChange(e) {
    var value = e.detail.value;
    var index = e.currentTarget.dataset.index; //
    if (index == 1) {
      console.log(thenS_.tabactive);
      if (thenS_.tabactive == 0) {
        thenS_.txdiagnosis_iinquiry_patient_allergy_details = value
      } else if (thenS_.tabactive == 1) {
        thenS_.txdiagnosis_log_main_suit = value
      } else if (thenS_.tabactive == 2) {
        thenS_.txdiagnosis_log_present_illness = value
      } else if (thenS_.tabactive == 3) {
        thenS_.txdiagnosis_log_history_present_illness = value
      } else if (thenS_.tabactive == 4) {
        thenS_.txdiagnosis_lof_diagnosis = value
      }
    } else if (index == 2) {
      thenS_.template_name = value
    } else if (index == 3) {
      thenS_.template_details = value
    } else if (index == 4) {
      thenS_.txdiagnosis_name = value
    } else if (index == 5) {
      // debugger
      // thenS_.inputtext = ''
      for (var i = 0; i < thenS_.diagnosis_list.length; i++) {
        if (thenS_.diagnosis_list[i].name == numText) {
          thenS_.diagnosis_list[i].name = thenS_.diagnosis_list[i].name + '(' + value + ')'
        }
      }
    } else if (index == 6) {
      thenS_.isdiagnosisText = value
    }
    this.setData({
      txdiagnosis_iinquiry_patient_allergy_details: thenS_.txdiagnosis_iinquiry_patient_allergy_details, //过敏史
      txdiagnosis_log_main_suit: thenS_.txdiagnosis_log_main_suit, //主诉
      txdiagnosis_log_present_illness: thenS_.txdiagnosis_log_present_illness, //现病史
      txdiagnosis_log_history_present_illness: thenS_.txdiagnosis_log_history_present_illness, //既往史
      txdiagnosis_lof_diagnosis: thenS_.txdiagnosis_lof_diagnosis, //诊断
      template_name: thenS_.template_name, //模板名称
      template_details: thenS_.template_details, //模板内容
      txdiagnosis_name: thenS_.txdiagnosis_name,
      inputtext: thenS_.inputtext,
      diagnosis_list: thenS_.diagnosis_list,
      activeIndex: '',
      isdiagnosisText: thenS_.isdiagnosisText
    })
    this.getdoctorSide_personal_inquiry()
  },
  txdiagnosis_taptab(e) {
    var index = e.currentTarget.dataset.index;
    var num = e.currentTarget.dataset.num;
    if (num == 1) {
      thenS_.tabactive = index
    } else if (num == 2) {
      console.log(index)
      thenS_.mould = index
    }
    this.setData({
      mould: thenS_.mould,
      tabactive: thenS_.tabactive
    })
    this.getdoctorSide_personal_inquiry()
  },
  taptemplateList(e) {
    var this_ = this
    var index = e.currentTarget.dataset.index; //下标
    var num = e.currentTarget.dataset.num; //1删除模板 2编辑模板 3 引用模板
    if (num == 1) {
      wx.showModal({
        title: '温馨提示',
        content: '确定要删除当前模板？',
        success(res) {
          if (res.confirm) {
            var jsonData = {
              "id": thenS_.templateList[index].id
            }
            app.http.deleteTemplate(jsonData).then(res => {
              this_.getdoctorSide_personal_inquiry()
            }).finally(res => {
              $service.showToast(res.msg)
            })
          }
        }
      })
    } else if (num == 2) { //编辑模板
      template_id = thenS_.templateList[index].id;
      if (thenS_.tabactive == "1") {
        var template_type = "主诉"
      } else if (thenS_.tabactive == "2") {
        var template_type = "现病史"
      } else if (thenS_.tabactive == "3") {
        var template_type = "既往史"
      }
      this.setData({
        txdiagnosis_isdetalis: true,
        txdiagnosis_isdetalistype: 2,
        template_type: template_type, //模板类型
        template_name: thenS_.templateList[index].templateName, //模板名称
        template_details: thenS_.templateList[index].templateDetails //模板内容
      })
    } else if (num == 3) {
      if (!$service.isUndefinedAndEmpty(thenS_.text)) {
        if (thenS_.text.indexOf(thenS_.templateList[index].templateDetails) > -1) {
          $service.showToast('请勿重复添加');
          return;
        }
        var valtext = thenS_.text + '、' + thenS_.templateList[index].templateDetails;
      } else {
        var valtext = thenS_.templateList[index].templateDetails;
      }
      if (thenS_.tabactive == 1) {
        thenS_.txdiagnosis_log_main_suit = valtext
      } else if (thenS_.tabactive == 2) {
        thenS_.txdiagnosis_log_present_illness = valtext
      } else if (thenS_.tabactive == 3) {
        thenS_.txdiagnosis_log_history_present_illness = valtext
      } else if (thenS_.tabactive == 4) {
        thenS_.txdiagnosis_lof_diagnosis = valtext
      }
      this.setData({
        txdiagnosis_iinquiry_patient_allergy_details: thenS_.txdiagnosis_iinquiry_patient_allergy_details, //过敏史
        txdiagnosis_log_main_suit: thenS_.txdiagnosis_log_main_suit, //主诉
        log_present_illness: thenS_.txdiagnosis_log_present_illness, //现病史
        txdiagnosis_log_history_present_illness: thenS_.txdiagnosis_log_history_present_illness, //既往史
        txdiagnosis_lof_diagnosis: thenS_.txdiagnosis_lof_diagnosis //诊断
      })
      this.getdoctorSide_personal_inquiry()
    }
  },
  txdiagnosis_tap_usage(e) {
    var num = e.currentTarget.dataset.num; //
    if (num == 1) {
      thenS_.txdiagnosis_islist2 = !thenS_.txdiagnosis_islist2
    } else {
      var index = e.currentTarget.dataset.index; //0主诉 1现病史 2既往史
      thenS_.template_type = index
      thenS_.txdiagnosis_islist2 = false
    }
    this.setData({
      template_type: thenS_.template_type,
      txdiagnosis_islist2: thenS_.txdiagnosis_islist2
    })
  },
  // ---------------- 填写诊断、主诉、过敏史等
  // 药物过敏史 保存接口
  updateAllergyHistory() {
    let params = {
      "id": inquiryPatientId,
      "patientAllergyDetails": thenS_.txdiagnosis_iinquiry_patient_allergy_details
    }
    app.http.updateAllergyHistory(params).then(res => {
      $service.showToast(res.msg);
      this.setData({
        'inquiryMap.patientAllergyDetails': thenS_.txdiagnosis_iinquiry_patient_allergy_details
      })
      this.publicUtil_getInquiryLogDetails();
    }).catch(err => {
      $service.showToast(err.msg);
    })
  },
  // 主诉 现病史 既往史 保存 接口
  updateInquiryLog() {
    // debugger
    let params = {
      "historyPresentIllness": thenS_.txdiagnosis_log_history_present_illness,
      "id": logId,
      "mainSuit": thenS_.txdiagnosis_log_main_suit,
      "presentIllness": thenS_.txdiagnosis_log_present_illness
    }
    app.http.updateInquiryLog(params).then(res => {
      $service.showToast(res.msg);
      this.setData({
        'inquiryMap.historyPresentIllness': thenS_.txdiagnosis_log_history_present_illness,
        'inquiryMap.mainSuit': thenS_.txdiagnosis_log_main_suit,
        'inquiryMap.presentIllness': thenS_.txdiagnosis_log_present_illness
      })
      this.publicUtil_getInquiryLogDetails();
    }).catch(err => {
      $service.showToast(err.msg);
    })
  },
  // 保存诊断
  updateDiagnosis(list) {
    let arr = []
    list.forEach((item) => {
      item.diagnosis = item.name
      arr.push(item.name)
    })
    let params = {
      "diagnosis": list,
      "id": logId,
    }
    app.http.updateDiagnosis(params).then(res => {
      $service.showToast(res.msg);
      this.data.inquiryMap.diagnosis = arr.join();
      this.setData({
        'inquiryMap.diagnosis': this.data.inquiryMap.diagnosis
      })
      this.publicUtil_getInquiryLogDetails();
    }).catch(err => {
      $service.showToast(err.msg);
    })
  },
  getalldo() { // 1查询病历 2检验检查 3健康指导意见单 4查询图片 5人体示意 6住院建议书 7问诊规范 8结束问诊 9开处方 10修改患者资料 11分诊  12常用语 13 测量数据 14查询历史问诊 15开处方-选择开药 16 开处方-生成处方 17检验检查-心电图 18检验检查-申请单  19健康指导-健康指导意见单详情填写 20填写诊断信息、主诉、过敏史等 21药品信息 22 保存模板 23处方详情 24 检验检查详情 25健康指导详情 26住院建议详情
    if (thenS_.openPageBox == 7) {
      this.doctorSide_personal_inquiry_getSpecification()
    }
    if (thenS_.openPageBox == 13) { //测量数据
      this.publicUtil_getPatientMeasuredDataList('now');
      this.publicUtil_getPatientMeasuredDataList('history');
    }
    if (thenS_.openPageBox == 15) { //15开处方-选择开药 
      if (thenS_.tapactive == 1 || thenS_.tapactive == 2) {
        // 药品目录和分类
        this.getDrugClass();
        this.getDrugList(0);
      } else if (thenS_.tapactive == 3) {
        this.data.paymentMethodNum == 0 ? this.getDrugList(1) : ''
      } else if (thenS_.tapactive == 4) {
        this.data.paymentMethodNum == 0 ? this.getPrescriptionTemplateList(0) : ''
      } else if (thenS_.tapactive == 5) {
        this.data.paymentMethodNum == 0 ? this.getPrescriptionTemplateList(1) : ''
      } else if (thenS_.tapactive == 6) {
        if (this.data.storeData.group_no != 0) {
          this.data.paymentMethodNum == 0 ? this.getOpenedPrescription() : ''
        }
      }
    }
  },
  // 获取药品类型
  getDrugClass() {
    app.http.getDrugClass().then(res => {
      res.data.unshift({
        id: '',
        className: '全部'
      })
      this.setData({
        drugClassList: res.data
      })
    }).catch(err => {
      $service.showToast(err.msg);
    })
  },
  // ---------------- 选择处方
  //西药、中药列表、分类，个人
  getDrugList(isPublic = 0) {
    var params = {
      "drugType": Number(thenS_.drug_type),
      "classId": thenS_.class_id,
      "current": Number(page),
      "drugName": thenS_.drug_name,
      "shopId": thenS_.shopId,
      "size": 50,
      "isPublic": String(isPublic),
    }
    app.http.getDrugList(params).then(res => {
      var drugList = res.data.list;
      if (drugList) {
        for (var i = 0; i < drugList.length; i++) {
          drugList[i].drug_usname = $service.nothingFun(drugList[i].drug_usname, '--')
        }
      }
      //判断选项卡情况
      if (thenS_.tapactive == 1) {
        if (page == 1) {
          thenS_.loadingText = '';
          thenS_.drugList = drugList;
          thenS_.loadingType = true;
        } else {
          if (!drugList || drugList.length == 0) {
            thenS_.loadingType = false;
            thenS_.loadingText = '没有更多了';
          } else {
            for (var i = 0; i < drugList.length; i++) {
              thenS_.drugList.push(drugList[i]);
              thenS_.loadingText = '加载更多';
            }
          }
        }
      } else {
        thenS_.loadingText = '';
        thenS_.drugList = drugList;
        thenS_.loadingType = true;
      }
      if (isPublic === 0) {
        this.setData({
          drugList: thenS_.drugList,
        })
      } else {
        this.setData({
          drugList2: thenS_.drugList,
        })
      }
      this.setData({
        loadingText: thenS_.loadingText,
        loadingType: thenS_.loadingType,
      })
    }).catch(err => {
      $service.showToast(err.msg);
    })
  },
  //点击按钮
  async tapdrugList(e) {
    var this_ = this;
    var type = e.currentTarget.dataset.type; //0目录、分类 1个人用药 2个人、公共模板 3历史处方
    var index = e.currentTarget.dataset.index; //下标
    this.setData({
      medListIndex: index
    })
    var num = e.currentTarget.dataset.num; //1保存用药
    thenS_.openPageBox_up = thenS_.openPageBox;
    if (num == 1) { //1保存用药
      console.log(this.data.drugList);
      app.http.addPersonalDrug({
        id: this.data.drugList[index].id,
        drugType: Number(this.data.drug_type)
      }).then(res => {
        $service.showToast(res.msg);
      }).catch(err => {
        $service.showToast(err.msg);
      })
    } else if (num == 2) { //详情
      if (type == 0) {
        var id = this.data.drugList[index].id
      } else if (type == 1) {
        var id = this.data.drugList2[index].id
      } else if (type == 4) {
        var id = this.data.diagnosisMap.drugList[index].id
      }
      thenS_.openPageBox = 21
      this.publicUtil_getDrugDetail(id)
    } else if (num == 3) { //开药
      if (type == 0) {
        var drugMap = this.data.drugList[index]
      } else if (type == 1) {
        var drugMap = this.data.drugList2[index]
      }
      thenS_.openPageBox = 16;
      console.log(type);
      console.log(drugMap);
      this.setData({
        drugMap: drugMap,
        sccfisshow: 1,
        details_usage: '', //用法
        details_consumption: '', //用量
        details_consumption_util: '', //用量单位
        details_frequency: '', ///频率,
        details_frequency_str: '',
        details_frequency_str2: '',
        details_day: '', //天数
        details_num: '', //盒数
        detailsEntrust: '', //嘱托
        islist: false, //弹窗显示隐藏
        islist2: false, //弹窗显示隐藏
        islist3: false, //弹窗显示隐藏
        isconsumptionlist: false, //弹窗显示隐藏
        drug_packaging_unit: '', //盒数单位
        getdruglistStoragelist: [], //已选处方
        prescription_price: 0, //处方金额
        prescription_taking: '', //服用方法
        prescription_requirements: '', //服用要求
        prescription_num: '', //付数
        prescription_isdetalis: false, //生成中药弹窗
        frequency_num: 0,
        frequency_day_num: 0,
        selectionunit: 0,
        drug_packaging_num: 1

      })
      this.getpageScrollTo()
      this.publicUtil_getInquiryLogDetails()
      this.publicUtil_getDrugDetail2('1') //药品详情
      this.getdruglistStorage()
    } else if (num == 4) { //删除个人用药/个人模板
      if (type == 1) {
        var drugMap = this.data.drugList2[index]
        console.log(drugMap);
        var content = '确定要删除当前常用药品？'
      } else if (type == 2) {
        var drugMap = this.data.drugList3[index]
        var content = '确定要删除当前常用模板？'
      }
      wx.showModal({
        title: '温馨提示',
        content: content,
        success(res) {
          if (res.confirm) {
            if (type == 1) { //删除个人用药
              app.http.deletePersonalDrug({
                id: thenS_.drugList2[index].drugTemplateId
              }).then(res => {
                page = 1
                $service.showToast(res.msg)
                this_.getalldo()
              }).catch(err => {
                $service.showToast(err.msg)
              })
            } else if (type == 2) { //删除个人模板
              console.log(thenS_.drugList3[index])
              app.http.deletePrescriptionTemplate({
                id: thenS_.drugList3[index].id
              }).then(res => {
                page = 1
                $service.showToast(res.msg)
                this_.getalldo()
              }).catch(err => {
                $service.showToast(err.msg)
              })
            }
          }
        }
      })
    } else if (num == 5 || num == 6 || num == 7) { //引用处方5 //查看模板6 7另存模板
      tapMap_type = 0
      if (type == 2) { //模板的
        thenS_.diagnosisMap = thenS_.drugList3[index]
      } else if (type == 3) { //历史处方的
        thenS_.diagnosisMap1 = thenS_.drugList4[index];
        thenS_.diagnosisMap.drugList = thenS_.drugList4[index].detailsResps
      }
      if (num == 5) {
        if (this.data.drug_type == 1) { //已选中 中药 、、存缓存
          druglistStorage_z = [];
          druglistStorage_z = type == 2 ? thenS_.drugList3[index].drugList : thenS_.drugList4[index].drugList
          wx.setStorageSync("druglistStorage_z" + doctorId, JSON.stringify(druglistStorage_z))
        } else { //已选中 西药 、、存缓存
          druglistStorage_x = [];
          druglistStorage_x = type == 2 ? thenS_.drugList3[index].drugList : thenS_.drugList4[index].drugList
          wx.setStorageSync("druglistStorage_x" + doctorId, JSON.stringify(druglistStorage_x))
        }
        $service.showToast('引用成功')
      } else if (num == 6) {
        tapMap_type = 1;
        this.setData({
          diagnosisMap: thenS_.diagnosisMap,
          isdetalis: true
        })
      } else if (num == 7) {
        tapMap_type = 2;
        thenS_.openPageBox_up = thenS_.openPageBox;
        this.setData({
          openPageBox: 22,
          openPageBox_up: thenS_.openPageBox_up,
          bcmb_type: 0
        })
        this.getpageScrollTo()
        template_prescriptionList = {
          detailsList: thenS_.drugList4[index].detailsResps,
          prescription_price: Number(thenS_.drugList4[index].prescriptionPrice).toFixed(2)
        }
        this.publicUtil_getUtilTable78()
      }
    } else if (num == 8) { //关闭弹窗
      this.setData({
        isdetalis: false
      })
    } else if (num == 9) {
      thenS_.openPageBox = 16
      this.getdruglistStorage()
      this.setData({
        sccfisshow: 0
      })
    }
    this.setData({
      openPageBox: thenS_.openPageBox,
      openPageBox_up: thenS_.openPageBox_up
    })
  },
  //药品详情
  publicUtil_getDrugDetail2: function (type) { // 1是开药 2是编辑
    var params = {
      "id": thenS_.drugMap.id
    }
    let api = this.data.paymentMethodNum == 1 ? 'medicineGetById' : 'getDrugInfo'
    app.http[api](params).then(res => {
      var drugMap = res.data;
      var drugMapList = this.data.drugMap;
      if (this.data.paymentMethodNum == 1) {
        drugMapList.goodsGuige = res.data.unit;
        drugMapList.goodsGuigeUnit = res.data.norms;
        drugMapList.drugSpecifications = res.data.norms;
        medicineObj = drugMap;
      } else {
        drugMapList.goodsGuige = res.data.goodsGuige;
        drugMapList.goodsGuigeUnit = res.data.goodsGuigeUnit;
        // this.data.drugList[index].goodsGuige = res.data.goodsGuige;
      }
      medicineObj = drugMap;
      if (type === '1') { // 开要设置默认用法
        this.publicUtil_getInquiryLoggetUtilTable() //工具
        this.setData({
          details_usage: this.data.drug_type == "1" ? drugMap.usageType : drugMap.drugUsage || "",
          details_consumption: drugMap.usageNum || "",
          details_consumption_util: drugMap.useUnit || drugMap.drugVolumeUnit,
          details_frequency_str2: drugMap.frequency || "",
          details_frequency: drugMap.frequencyId || "",
          details_frequency_str: drugMap.frequencyName || "",
          frequency_num: drugMap.frequencyNum || "", // 频次数
          frequency_day_num: drugMap.frequencyTime || "", // 频率时间间隔 
          detailsEntrust: drugMap.enjoin || "",
          details_day: drugMap.drugMaxday || "",
          selectionunit: drugMap.drugUnit ? '5' : '2' // 如果药品单位有则使用5的数据计算药品 没有则用2
        })
        if (this.data.drug_type == "2") this.jisuanxiyaochufangshuliang()
      }
      this.setData({
        consumptionlist: drugMap.consumptionList,
        drug_packaging_unit: drugMap.drugPackagingUnit,
        useUnit: drugMap.useUnit,
        drugMap: drugMapList,
        dosage: this.data.paymentMethodNum == 1 ? drugMap.dosage : '',
      })
    }).catch(err => {
      $service.showToast(err.msg)
    })
  },
  getdruglistStorage() {
    if (thenS_.drug_type == 1) { //1中药
        if (!$service.isUndefinedAndEmpty(wx.getStorageSync("druglistStorage_z" + doctorId))) {
            var list = $service.strToJson(wx.getStorageSync("druglistStorage_z" + doctorId));
        } else {
            var list = []
        }
        druglistStorage_z = list; //已选中药处方 列表

    } else if (thenS_.drug_type == 2) {
        if (!$service.isUndefinedAndEmpty(wx.getStorageSync("druglistStorage_x" + doctorId))) {
            var list = $service.strToJson(wx.getStorageSync("druglistStorage_x" + doctorId));
        } else {
            var list = []
        }
        druglistStorage_x = list; //已选西药处方 列表
    }
    var drug_price = 0;
    for (var i = 0; i < list.length; i++) {
        if ($service.isUndefinedAndEmpty(list[i].drugPrice)) {
            list[i].drugPrice = 0
        }
        drug_price += $service.strToFloat(list[i].drugPrice) * $service.strToInt(list[i].detailsNum);
    }
    this.setData({
        getdruglistStoragelist: list,
        prescription_price: Number(drug_price).toFixed(2)
    })
},



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    thenS_ = this.data;
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
})